package com.titan.demo.camel.sample.数据转换.unmarshal方法;

import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;

import com.titan.demo.camel.utils.RunCamel;
import com.titan.demo.camel.utils.processor.PrintClassProcessor;

public class Test01 {
	public static void main(String[] args) {
        try {
        	RouteBuilder route = createRouteBuilder();
        	createRunCamel().run(route);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	private static RunCamel createRunCamel() {
		return new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				String input = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
				producer.sendBody("direct:in", input);
			}
		};
	}
	
	private static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
			public void configure() throws Exception {
                from("direct:in")
                .to("stream:out")
                .process(new PrintClassProcessor("${body}"))
                .unmarshal().json(JsonLibrary.Gson, Map.class)
                .process(new PrintClassProcessor("${body}"))
                .to("stream:out")
                ;
            }
        };
    }
}



